/* eslint-disable @typescript-eslint/no-use-before-define */
import services from '@/services/login';
import { PageContainer } from '@ant-design/pro-components';
import { Access, history, useAccess } from '@umijs/max';
import { Button, Form, Input, message } from 'antd';
import { useIntl } from 'umi';
const { checkUser } = services.LoginAPI;

interface UserType {
  username?: string;
  password?: string;
}
const Login: React.FC<UserType> = () => {
  const access = useAccess();
  const intl = useIntl();
  const t = (item: string) => intl.formatMessage({ id: `${item}` });
  const [messageApi, contextHolder] = message.useMessage();

  return (
    <PageContainer>
      <Access accessible={access.canSeeAdmin}>
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={handleCliek}
          autoComplete="off"
        >
          {contextHolder}
          <Form.Item
            label={t('Label.Username')}
            name="username"
            rules={[
              {
                required: true,
                message: t('Message.PleaseInpt') + t('Label.Username'),
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label={t('Label.Password')}
            name="password"
            rules={[
              {
                required: true,
                message: t('Message.PleaseInpt') + t('Label.Password'),
              },
            ]}
          >
            <Input.Password />
          </Form.Item>
          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Button type="primary" block htmlType="submit">
              {t('Label.Login')}
            </Button>
          </Form.Item>
        </Form>
      </Access>
    </PageContainer>
  );
  async function handleCliek({ username, password }: UserType) {
    if ((username && password) !== 'admin') {
      messageApi.open({
        type: 'warning',
        content: '用户名和密码为：admin',
      });
      return false;
    }
    try {
      const { data, success } =  await checkUser({
        username: username || '',
        password: password || '',
      });
      const {loginUser,loginPwd} = data?.id
      console.log(loginUser,loginPwd);
      return loginUser && loginPwd === "admin" ?  history.push('/home') :  success
    } catch (error) {
      console.log(error);
      return false;
    }
  }
};

export default Login;
